/************************************************************************************/
/*  Program name CAPFAEXT_VALUES.sql                                               */
/*  Creater:  NIP Karen (PCCW - UniHub)                                             */
/*************   History   *************************                                */
/* 9 JAN, 2005   NIP Karen         First created                                    */
/************************************************************************************/
ALTER SESSION SET NLS_DATE_FORMAT='YYYYMMDD HH24:MI:SS';
SET SERVEROUTPUT ON SIZE 1000000;
SET VERIFY OFF

DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPFAEXT_VALUES';

	lv_Scc_Count	NUMBER			:=	0;
	lv_Err_Count	NUMBER			:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	lv_FileName		VARCHAR2(100)	:=	'CAPFA_VALUES.dat';
	lv_PathName		VARCHAR2(100)	:=	'/tmp';
	lv_UTL_File		UTL_FILE.File_Type;

	lv_NGO_Name		VARCHAR2(100);
	lv_NGO_Count	NUMBER;

	CURSOR Cur_F(p_NGO VARCHAR2) IS
	SELECT
		FFV.*
	FROM
		FND_FLEX_VALUES			FFV
	WHERE	SUBSTR(FFV.Flex_Value,1,4)	=	p_NGO;
	

BEGIN
	lv_NGO_Name		:=	UPPER('&1');

	SELECT	COUNT('A')
	INTO	lv_NGO_Count
	FROM	HR_ALL_ORGANIZATION_UNITS
	WHERE	SUBSTR(Name,4,4)	=	lv_NGO_Name;

	IF	lv_NGO_Count	>=	1	THEN
		lv_UTL_File		:=	UTL_FILE.FOpen(lv_PathName, lv_FileName, 'w');
		FOR C1 IN Cur_F(lv_NGO_Name) LOOP
			UTL_FILE.PUT_LINE(
				lv_UTL_File,
				C1.Flex_Value_Set_ID||CHR(9)||
				C1.Flex_Value_ID||CHR(9)||
				C1.Flex_Value||CHR(9)||
				C1.Last_Update_Date||CHR(9)||
					C1.Last_Updated_By||CHR(9)||
				C1.Creation_Date||CHR(9)||
					C1.Created_By||CHR(9)||
				C1.Last_Update_Login||CHR(9)||
				C1.Enabled_Flag||CHR(9)||
				C1.Summary_Flag||CHR(9)||
				C1.Start_Date_Active||CHR(9)||
				C1.END_DATE_ACTIVE||CHR(9)||
				C1.PARENT_FLEX_VALUE_LOW||CHR(9)||
				C1.Parent_Flex_Value_High||CHR(9)||
				C1.Structured_Hierarchy_Level||CHR(9)||
				C1.HIERARCHY_LEVEL||CHR(9)||
				REPLACE(C1.Compiled_Value_Attributes,CHR(10),'^C10^')||CHR(9)||
				C1.Value_Category||CHR(9)||
				C1.Attribute1||CHR(9)||
				C1.Attribute2||CHR(9)||
				C1.Attribute3||CHR(9)||
				C1.Attribute4||CHR(9)||
				C1.Attribute5||CHR(9)||
				C1.Attribute6||CHR(9)||
				C1.Attribute7||CHR(9)||
				C1.Attribute8||CHR(9)||
				C1.Attribute9||CHR(9)||
				C1.Attribute10||CHR(9)||
				C1.Attribute11||CHR(9)||
				C1.Attribute12||CHR(9)||
				C1.Attribute13||CHR(9)||
				C1.Attribute14||CHR(9)||
				C1.Attribute15||CHR(9)||
				C1.Attribute16||CHR(9)||
				C1.Attribute17||CHR(9)||
				C1.Attribute18||CHR(9)||
				C1.Attribute19||CHR(9)||
				C1.Attribute20||CHR(9)||
				C1.Attribute21||CHR(9)||
				C1.Attribute22||CHR(9)||
				C1.Attribute23||CHR(9)||
				C1.Attribute24||CHR(9)||
				C1.Attribute25||CHR(9)||
				C1.Attribute26||CHR(9)||
				C1.Attribute27||CHR(9)||
				C1.Attribute28||CHR(9)||
				C1.Attribute29||CHR(9)||
				C1.Attribute30||CHR(9)||
				C1.Attribute31||CHR(9)||
				C1.Attribute32||CHR(9)||
				C1.Attribute33||CHR(9)||
				C1.Attribute34||CHR(9)||
				C1.Attribute35||CHR(9)||
				C1.Attribute36||CHR(9)||
				C1.Attribute37||CHR(9)||
				C1.Attribute38||CHR(9)||
				C1.Attribute39||CHR(9)||
				C1.Attribute40||CHR(9)||
				C1.Attribute41||CHR(9)||
				C1.Attribute42||CHR(9)||
				C1.Attribute43||CHR(9)||
				C1.Attribute44||CHR(9)||
				C1.Attribute45||CHR(9)||
				C1.Attribute46||CHR(9)||
				C1.Attribute47||CHR(9)||
				C1.Attribute48||CHR(9)||
				C1.Attribute49||CHR(9)||
				C1.Attribute50||CHR(9)||
				CAPBREXT_COMMON.GetUser(C1.LAST_UPDATED_BY)||CHR(9)||
				CAPBREXT_COMMON.GetUser(C1.CREATED_BY)
			);

			lv_Scc_Count	:=	lv_Scc_Count	+	1;
		END LOOP;
		UTL_FILE.FClose(lv_UTL_File);
	ELSE
		lv_Err_Count	:=	lv_Err_Count + 1;
		lv_Err_Msg		:=	lv_NGO_Name||': No Flex Values for this NGO';
	END IF;

	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);

	COMMIT;
END;
/
